<?php

namespace App\Admin\Controllers;

use App\Models\Cabinet;
use App\Models\CabinetAlarm;
use App\Models\CabinetPoint;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
// use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use App\Models\ProjectUser;
use App\Models\Project;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use App\Admin\Actions\Batch\CopyCabinetAlarm;
use App\Admin\Actions\Batch\CopyCabinetAlarmAcrossBox;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Layout\Column;
use App\Admin\Actions\Grid\AlarmSettingUpload;

class CabinetAlarmController extends Controller
{

    public function index(Content $content, $boxId = 0)
    {
        Admin::script(
            <<<JS
    var table = document.getElementById('grid-table');
    table.style.cssText="background-color:#e3e5eb;"
JS            
        );
        $row = new Row;
        $row->column(12, function (Column $column) use($boxId) {
            $column->row(function(Row $row) use($boxId) {
                $alramCountData = $this->getAlramCountData($boxId);
                $row->column(8, view('admin.card.alarmCount', $alramCountData));
                $alramLevelCountData = $this->getAlramLevelCountData($boxId);
                $row->column(4, view('admin.card.alarmLevelCount', $alramLevelCountData));
            });
        });
        return $content
            ->body(headmenu($boxId,4))
            ->body($row)
            // ->body(view('admin.cabinet.search',['autoRefresh' => '123', 'key' => '321']))
            ->body($this->grid($boxId));
    }

    protected function grid($boxId)
    {
        $optionBoxes = Cabinet::whereIn('id',[$boxId])->pluck('name', 'id')->toArray();
        $optionCabinets = Cabinet::where('pid', $boxId)->where('show', Cabinet::SHOW_YES)->pluck('name', 'id')->toArray();
        $cabinets = Cabinet::where('pid', $boxId)->pluck('id')->toArray();
        $optionPoints = CabinetPoint::whereIn('cabinet_id',$cabinets)->pluck('name', 'id')->toArray();
        $m = CabinetAlarm::where('cabinet_pid', $boxId);
        return new Grid($m, function (Grid $grid) use ($optionBoxes,$optionCabinets, $optionPoints, $boxId) {
            // $grid->header(function ($collection) {
            //     return view('admin.cabinet.search',['autoRefresh' => '123', 'key' => '321']);
            // });
            $grid->column('cabinet_pid', '云盒')->using($optionBoxes);
            $grid->column('cabinet_id', '设备')->using($optionCabinets);
            $grid->column('point_id', '采集点')->using($optionPoints);
            $grid->column('name','报警名称');
            $grid->column('level', '报警等级')->using(CabinetAlarm::LEVEL_TEXT);
            $grid->column('condition', '条件')->using(CabinetAlarm::CONDITION_TEXT);
            $grid->column('value','报警值');
            $grid->column('active', '状态')->using(CabinetAlarm::ACTIVE_TEXT);
            $grid->column('map_alarm', '地图报警')->using(CabinetAlarm::ACTIVE_TEXT);
            
            $grid->batchActions(function ($batch) {
                $batch->add(new CopyCabinetAlarm('<i class="fa fa-copy"></i>&nbsp;复制'));
                $batch->add(new CopyCabinetAlarmAcrossBox());
            });

            $grid->filter(function (Grid\Filter $filter) use ($optionCabinets) {
                $filter->panel();
                $filter->equal('cabinet_id', '设备')->select($optionCabinets)->load('point_id', '/api/point_id')->width(2);
                $filter->equal('point_id', '采集点')->select()->load('alarm_id', '/api/alarm_id')->width(2);
                $filter->equal('level', '报警等级')->select(CabinetAlarm::LEVEL_TEXT)->width(2);
                $filter->equal('active', '状态')->select(CabinetAlarm::ACTIVE_TEXT)->width(2);
                $filter->equal('map_alarm', '地图报警')->select(CabinetAlarm::ACTIVE_TEXT)->width(2);
            });

            $grid->tools(AlarmSettingUpload::make()->setKey($boxId));

        });
    }

    public function update($boxId, $id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content, $boxId){
        $box = Cabinet::find($boxId);
        return $content
            ->title('新建报警')
            ->description($box['name'])
            ->body($this->form($boxId));
    }

    public function form($boxId = 0){
        $box = Cabinet::find($boxId);
        return Form::make(new CabinetAlarm(), function (Form $form) use ($box) {
            // $form->select('cabinet_pid', '云盒')->options($optionBoxes)->load('cabinet_id', '/api/cabinet_id')->required();
            // $form->select('cabinet_id', '柜子')->load('point_id', '/api/point_id')->required();
            $cabinets = $box ? Cabinet::where('pid', $box['id'])->pluck('name', 'id')->toArray() : [];
            $form->select('cabinet_id', '柜子')->options($cabinets)->load('point_id', '/api/point_id')->required();
            $form->text('name', '报警名称')->required();
            $form->select('point_id', '采集点')->required();
            $form->select('condition', '条件')->options(CabinetAlarm::CONDITION_TEXT)->required();
            $form->text('value', '报警值')->required();
            $form->radio('level', '报警等级')->options(CabinetAlarm::LEVEL_TEXT)->default(1);
            $form->switch('active', '是否启用')->default(0);
            $form->switch('map_alarm', '地图报警')->default(0);
            $cabinetPid = $box ? $box['id'] : 0;
            $projectId = $box ? $box['project_id'] : 0;
            $form->hidden('cabinet_pid')->value($cabinetPid);
            $form->hidden('project_id')->value($projectId);
        

            // $form->saving(function (Form $form) {
            //     $form->project_id = Cabinet::where('id', $form->cabinet_pid)->value('project_id');
            // });
        });
    }

    public function edit($boxId, $id, Content $content){
        $box = Cabinet::find($boxId);
        return $content
            ->title('云盒编辑')
            ->description($box['name'])
            ->body($this->form($boxId)->edit($id));
    }

    public function destroy($boxId, $id)
    {
        return $this->form()->destroy($id);
    }

    public function show($boxId, $id, Content $content){
        $one = CabinetAlarm::find($id);
        $box = Cabinet::find($boxId);
        $cabint = Cabinet::find($one['cabinet_id']);
        return $content
            ->translation($cabint['name'])
            ->title($cabint['name'])
            ->description($box['name'])
            ->body($this->detail($id));
    }

    public function detail($id){
        return Show::make($id, new CabinetAlarm(), function (Show $show) {
            $show->field('name','名称');
            $show->field('condition','条件')->using(CabinetAlarm::CONDITION_TEXT);
            $show->field('value','报警值');
        });
    }

    public function cabinet_id(Request $request)
    {
        $boxId = $request->get('q');

        return Cabinet::where('pid', $boxId)->get(['id', DB::raw('name as text')]);
    }

    public function point_id(Request $request)
    {
        $cabinetId = $request->get('q');

        return CabinetPoint::where('cabinet_id', $cabinetId)->get(['id', DB::raw('name as text')]);
    }

    protected function getAlramCountData($boxId){
        $cabinets = Cabinet::where('pid', $boxId)->where('show', Cabinet::SHOW_YES)->get()->toArray();
        $xData = [];
        $yData = [];
        foreach ($cabinets as $oneCabinet) {
            $xData[] = $oneCabinet['name'];
            // $yData[] = CabinetAlarm::where('cabinet_id', $oneCabinet['id'])->count();
            $yData[] = rand(0, 100);
        }
        
        $data['xData'] = json_encode($xData);
        $data['yData'] = json_encode($yData);
        return $data;
    }

    protected function getAlramLevelCountData($boxId){
        $levels = CabinetAlarm::LEVEL_TEXT;
        $alarmLevelData = [];
        foreach ($levels as $level => $levelName) {
            // $one['value'] = CabinetAlarm::where('cabinet_pid', $boxId)->where('level', $level)->count();
            $one['value'] = rand(0,50);
            $one['name'] = $levelName;
            array_push($alarmLevelData, $one);
        }
        $data['data'] = json_encode($alarmLevelData);
        return $data;
    }
}